#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import flujo_entrada
from catlex import *
import error
import string
import sys

class analizador_lexico:
    def __init__(self, entrada):
        self.entrada = entrada
        self.comp = None
        self.lexema = ""
        self.estado = 1
        self.caracter = 0
        self.linea = 1
        self.avanza()
        
    def siguiente_componente(self):
        return self.comp
    
    def avanza(self):
        c = self.entrada.siguiente_caracter()
        self.estado = 1
        self.lexema = ""
        while 1:
            if self.estado == 1:
                if c in [" ", "\t"]:
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 7
                elif c >= "0" and c <= "9":
                    self.lexema = self.lexema + c
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 4
                elif c == "+":
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 2
                elif c == "*":
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 3
                elif c == "(":
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 5
                elif c == ")":
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 6
                elif c == "\n":
                    self.entrada.avanza()
                    self.linea = self.linea + 1
                    self.caracter = 1
                    c = self.entrada.siguiente_caracter()
                    self.estado = 8
                elif c == "": # EOF
                    self.estado = 9
                else:
                    raise error.ErrorLexico("carácter desconocido en la posición %d" % (self.caracter + 1))
                
            elif self.estado == 2:
                self.comp = suma(self.linea, self.caracter)
                break
            
            elif self.estado == 3:
                self.comp = producto(self.linea, self.caracter)
                break
            
            elif self.estado == 4:
                if c >= "0" and c <= "9":
                    self.lexema = self.lexema + c
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
                else:
                    self.comp = entero(string.atoi(self.lexema), self.linea, self.caracter)
                    break
                
            elif self.estado == 5:
                self.comp = abre(self.linea, self.caracter)
                break
            
            elif self.estado == 6:
                self.comp = cierra(self.linea, self.caracter)
                break
            
            elif self.estado == 7:
                if c not in [" ", "\t"]:
                    self.lexema = ""
                    self.estado = 1
                else:
                    self.entrada.avanza()
                    self.caracter = self.caracter + 1
                    c = self.entrada.siguiente_caracter()
            
            elif self.estado == 8:
                self.comp = nl(self.linea, self.caracter)
                break
            
            elif self.estado == 9:
                self.comp = eof(self.linea, self.caracter)
                break
                

if __name__ == "__main__":
    fe = flujo_entrada.cadena("(((5 + 4))+4 * 45l6))) * 4")
    a = analizador_lexico(fe)
    try:
        while a.siguiente_componente().cat != cat.eof:
            print a.siguiente_componente()
            a.avanza()
    except error.Error, err:
        sys.stderr.write("%s\n" % err)
        
        